package cn.zch.soft.util.qrcode.utils;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import cn.zch.soft.util.qrcode.img.MyMatrixToImageConfig;
import cn.zch.soft.util.qrcode.img.MyMatrixToImageWriter;

import java.util.Hashtable;
public class QrCodeEncoderHandler {
  /**
   * @param contents 生成的图片的名称
   * @param width    生成的图片的宽度
   * @param height   生成的图片的高度
   * @param imgPath  生成的图片路径
   * @param onColor  qrCode的颜色
   * @param offColor qrCode背景颜色
   */
  public void encode(String contents, int width, int height, String imgPath, String logoPath, int onColor, int offColor, int i, String format, int devicename) {
    Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
    // 指定纠错等级
    hints.put(EncodeHintType.PDF417_DIMENSIONS, ErrorCorrectionLevel.H);
    // 指定编码格式
    hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
    try {
      if (devicename == 1) {
        hints.put(EncodeHintType.MARGIN, 1);
        // 设置生成二维码的类型
        BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height + 70, hints);
        //bitMatrix = deleteWhite(bitMatrix, 0);
        // 0xFFE30022 字体色,0xFFF4C2C2背景色
        MyMatrixToImageWriter.writeToFile(bitMatrix, format, imgPath, new MyMatrixToImageConfig(onColor, offColor), logoPath, i);
        /*---------*/
        CutImg.cutImage(imgPath, imgPath, 0, 0, width, height + 30, format);
        /*---------*/
      }
      else {
        hints.put(EncodeHintType.MARGIN, 0);
        // 设置生成二维码的类型
        BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height, hints);
        //bitMatrix = deleteWhite(bitMatrix, 0);
        // 0xFFE30022 字体色,0xFFF4C2C2背景色
        MyMatrixToImageWriter.writeToFiles(bitMatrix, format, imgPath, new MyMatrixToImageConfig(onColor, offColor), logoPath, i);
        /*---------*/
        //CutImg.cutImage(imgPath, imgPath, 0, 0, width, height,format);
        /*---------*/
      }
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }
  /*---------------*/
  public static BitMatrix deleteWhite(BitMatrix matrix, int addX) {
    int[] rec = matrix.getEnclosingRectangle();
    int resWidth = rec[2] + addX;
    int resHeight = rec[3] + addX;
    BitMatrix resMatrix = new BitMatrix(resWidth, resHeight);
    resMatrix.clear();
    for (int i = 0; i < resWidth; i++) {
      for (int j = 0; j < resHeight; j++) {
        if (matrix.get(i + rec[0], j + rec[1])) {
          resMatrix.set(i, j);
        }
      }
    }
    return resMatrix;
  }
  /*---------------*/
  public static void main(String[] args) {
    // String imgPath = "E:/zxing.png";
    // String logoPath = "D:/pic/jpg/warming_up.jpg";
    // String contents = "testPNG";
    // int width = 200, height = 200;
    // QrCodeEncoderHandler handler = new QrCodeEncoderHandler();
    // handler.encode(contents, width, height, imgPath, logoPath, 0x74A327, 0xFFFFFFF,1);
  }
}
